/*
 * The MIT License
 *
 * Copyright 2014 David Rodenas Pico.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.uoc.sd.fr14.actors;

import java.util.Set;

import edu.uoc.sd.fr14.mesg.Message;
import edu.uoc.sd.fr14.model.Quorum;
import edu.uoc.sd.fr14.model.ServerId;

/**
 *
 * @author drodenas
 */
public interface Cluster {

	/**
	 * Gets the clock for the current node to seat/cancel tiemouts.
	 * <p>
	 * 
	 * When a server wants to add a timeout 
	 * it will request the clock to this Cluster
	 * and perform the required set/cancel timeout.
	 * <p>
	 * 
	 * You should fill the from field of each call
	 * with the current server id.
	 * 
	 * @return the clock in this node
	 */
    Clock getClock();

    /**
     * Gets the quorum object that computes if a majority of servers has did something.
     * <p>
     * 
     * You should use this quorum object to know if you have 
     * enough servers that agree.
     *  
     * @return the quorum instance
     */
    Quorum getQuorum();

    /**
     * Sends a message to a customer.
     * <p>
     * 
     * You should instance a message valid for a customer
     * with the customerId and other info and call with it.
     * <p>
     * 
     * Example:
     * <code>
     * cluster.sendToCustomer(new ConfirmedMessage(customer, flight, customer, confirmed));
     * </code>
     * 
     * @param message a new message instance that you want to send to a customer actor
     */
    void sendToCustomer(Message message);

    /**
     * Sends a message to a customer either to a server.
     * <p>
     * 
     * This method allows to send messages that are valid to servers or to customers,
     * like for example the confirmed operation. 
     * <p>
     * 
     * You should create a message with a valid proxy target (server or customer),
     * and with required data.
     * <p>
	*
     * Example:
     * <code>
     * cluster.sendToProxy(new ConfirmedMessage(proxy, flight, customer, confirmed));
     * </code>
     * 
     * @param message a new message instance that you want send to a customer or to a server
     */
    void sendToProxy(Message message);

    /**
     * Sends a message to another server.
     * <p>
     * 
     * This method allows to send new messages valid for any server
     * with the server id and other information.
     * <p>
     * 
     * You should create a new instance of a message kind that you want
     * to send with the required values set.
     * <p>
     *  
     * Example:
     * <code>
     * cluster.sendToServer(new SeatMessage(leader, flightId, customer, me));
     * </code>
     * 
     * @param message a new message instance that you want send to a server
     */
    void sendToServer(Message message);

    /**
     * Gets the list of available servers.
     * <p>
     * 
     * If you need to broadcast a message, 
     * or send a message to many servers you 
     * can obtain the server list by using this method.
     * <p>
     * 
     * If you want to send a message to other servers
     * you should first check that the server id that
     * you will use is not you. 
     * If you want to send a message to greater id servers
     * you should use the operation of ServerId isGreaterThan
     * to check that you want to use this server id.
     * <p>
     * 
     * You should not modify or save this list, 
     * it may change if new servers are added.
     * You should request and use the value only for
     * a limited period of time.
     * 
     * @return the set of all server ids (including you)
     */
    Set<ServerId> getServers();
    
}
